package com.xn.app.muying.activity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xn.app.muying.R;
import com.xn.app.muying.sqlite.DBManager;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.BaseActivity;
import com.xn.app.muying.util.Constants;
import com.xn.app.muying.util.JSONUtil;
import com.xn.app.muying.util.MyAlertDialog;
import com.xn.app.muying.util.ToastUtil;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

public class ChongZhiMiMaActivity extends BaseActivity {
    private String TAG=this.getClass().getSimpleName();
    private EditText chong_zhi_mi_ma_1, chong_zhi_mi_ma_2;
    private Button chong_zhi_mi_ma;
    //数据库
    private DBManager db = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chong_zhi_mi_ma);
        db=new DBManager(this);
        chong_zhi_mi_ma_1 = (EditText) findViewById(R.id.chong_zhi_mi_ma_1);
        chong_zhi_mi_ma_2 = (EditText) findViewById(R.id.chong_zhi_mi_ma_2);
        chong_zhi_mi_ma= (Button) findViewById(R.id.chong_zhi_mi_ma);
        findViewById(R.id.guan_bi).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        chong_zhi_mi_ma.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mm1 = chong_zhi_mi_ma_1.getText().toString();
                String mm2 = chong_zhi_mi_ma_2.getText().toString();
                if (!TextUtils.isEmpty(mm1) && !TextUtils.isEmpty(mm2)) {
                    if (TextUtils.equals(mm1, mm2)) {
                        xiugai(mm1);
                    } else {
                        ToastUtil.showToast(ChongZhiMiMaActivity.this, "密码和确认密码不一样，请检查");
                    }
                } else {
                    ToastUtil.showToast(ChongZhiMiMaActivity.this, "密码和确认密码不能为空");
                }
            }
        });
    }


    AlertDialog zhuce_dialog;

    void xiugai(final String mima) { 
        zhuce_dialog = new MyAlertDialog().ShowDialog(ChongZhiMiMaActivity.this, R.layout.dialog_loading);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String yz = Constants.getStringByName(ChongZhiMiMaActivity.this, "YanZheng");
                    Type type = new TypeToken<HashMap<String, String>>() {
                    }.getType();
                    HashMap<String, String> msg = new Gson().fromJson(yz, type);
                    HashMap<String, String> param = new HashMap<>();
                    param.put("newPassword", mima);
                    param.put("securityCodeToken", msg.get("token"));
                    String p=new Gson().toJson(param);
                    RequestBody formBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), p);
                    Request request = new Request.Builder().url(APIMethod.XiuGaiMiMa).addHeader("Authorization",db.select("token_type")+" " + db.select("access_token")).post(formBody).build();
                    okhttp3.Response response = MuYingApplication.mOkHttpClient.newCall(request).execute();
                    int code = response.code();
                    String bodys = response.body().string();
                    if (code == 204||code==200) {
                        Message msg2 = new Message(); 
                        msg2.what = 0;
                        msg2.obj = bodys;
                        handler.sendMessage(msg2);
                    } else {
                        Message msg1 = new Message();
                        msg1.what = 1;
                        msg1.arg1=code;
                        msg1.obj = bodys;
                        handler.sendMessage(msg1);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    zhuce_dialog.dismiss();
                    ToastUtil.showToast(ChongZhiMiMaActivity.this, "修改成功");
                    finish();
                    break;
                case 1:
                    zhuce_dialog.dismiss();
                    if (400<=msg.arg1&&msg.arg1<500){
                        ToastUtil.showToast(ChongZhiMiMaActivity.this, "修改失败");
                    }else{
                        try {
                            JSONObject jsonObject = JSONUtil.strToJson(msg.obj.toString());
                            jsonObject=JSONUtil.strToJson(jsonObject.get("error").toString());
                            ToastUtil.showToast(ChongZhiMiMaActivity.this, jsonObject.get("message").toString());
                        }catch (Exception e){
                            ToastUtil.showToast(ChongZhiMiMaActivity.this,"修改失败！");
                        }
                    }
                    break;
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
    }
}
